import {SAVE_USER_INFO, DELETE_USER_INFO} from '../action_types.js'

// 取localStorage中信息
let user = JSON.parse(localStorage.getItem('user'))
let token = localStorage.getItem('token')
let isLogin = user && token ? true : false

let initState = {
  user: user || '',
  token: token || '',
  isLogin: isLogin
};

export default function (preState=initState, action) {
  let {type,data} = action
  switch (type) {
    case SAVE_USER_INFO:
      return {user:data.user, token:data.token, isLogin:true};
    case DELETE_USER_INFO:
      return {user:'', token:'', isLogin: false};
    default:
      return preState
  }
}
